package com.retlife.tvshow.serial_port.bike

import android.util.Log
import com.retlife.tvshow.serial_port.BaseSerialPoster
import com.retlife.tvshow.serial_port.BaseSerialReceiver

class BaseSerialBikeReceiver : BaseSerialReceiver() {

    override fun connectMsg(path: String?, isSucc: Boolean) {
        if (isSucc) {
            startCheck()
        }
    }

    override fun checkData() {
        if (bufferLen > 9) {
            if (dataBuffer[0] != Head || dataBuffer[1] != Head2) {
                for (i in 1 until (bufferLen - 5)) {
                    if (dataBuffer[i] == Head && dataBuffer[i + 1] == Head2) {
                        bufferLen -= i
                        System.arraycopy(dataBuffer, i, dataBuffer, 0, bufferLen)
                        break
                    }
                }
                if (dataBuffer[0] != Head || dataBuffer[1] != Head2){
                    System.arraycopy(dataBuffer, bufferLen - 5, dataBuffer, 0, 5)
                    bufferLen= 5
                }
            }
            if (bufferLen > 5) {
                val contentSize: Int = dataBuffer[2].toInt()
                val dataSize: Int = contentSize + 3
                if (dataSize > bufferLen) {
                    Log.i(javaClass.simpleName, "串口数据长度不够")
                } else {
                    val bytes = ByteArray(dataSize)
                    System.arraycopy(dataBuffer, 0, bytes, 0, dataSize)
                    bufferLen -= (dataSize)
                    System.arraycopy(dataBuffer, dataSize, dataBuffer, 0, bufferLen)
                    if (!checkCrc(bytes)) {
                        Log.i(javaClass.simpleName, "校验位出错,丢弃数据")
//                        System.arraycopy(ByteArray(1024), 0, dataBuffer, 0, 1024)
//                        dataBuffer = ByteArray(1024)
//                        bufferLen = 0
                        return
                    }
                    Log.i(
                        javaClass.simpleName,
                        "receive::" + BaseSerialPoster.bytes2hex(bytes)
                    )
                    translateContent(bytes)
                }
            }
        } //            Log.i(getClass().getSimpleName(), "串口数据长度不够");
    }

    private fun translateContent(bytes: ByteArray) {
        when (val cmd: Int = (((bytes[3].toInt()and 0xff) shl 8) or (bytes[4].toInt()and 0xff))) {
//        when (val cmd: Int = Util.commandCode(bytes)) {
        /*    DecodeData.NUT_BIKE_TORQUE_DATA_CODE -> {
                val bikeData: BikeData = DecodeData.decodeData(bytes)
                EventBus.getDefault()
                    .post(EventBusMsg(EventBusOrder.BIKE_DATA_CALL_BACK, bikeData))
            }

            DecodeData.NUT_BIKE_TORQUE_STATUS_CODE -> {
                val bikeState: BikeState = DecodeData.decodeState(bytes)
                EventBus.getDefault()
                    .post(EventBusMsg(EventBusOrder.BIKE_STATE_CALL_BACK, bikeState))
            }
            DecodeData.NUT_POWER_STATUS_CODE -> {

            }
            NUT_BIKE_SYSTEM_SETTING_CODE->{
                val systemSettingBean = DecodeData.decodeSystemSetting(bytes)
                EventBus.getDefault()
                 .post(EventBusMsg(EventBusOrder.BIKE_SYSTEM_SETTING_CALL_BACK,systemSettingBean))
            }
            else -> {
                Log.i(javaClass.simpleName, "未知命令码:$cmd")
            }*/
        }
    }

    private fun checkCrc(bytes: ByteArray): Boolean {
        val crc: Int = SerialBikeManager.crc(bytes) and 0xffff
//        val crc = Util.crc(bytes)
        val toInt = (bytes[bytes.size - 1]).toInt() and 0xff
        val toInt1 = bytes[bytes.size - 2].toInt() and 0xff
        val i = crc and 0xff
        val i1 = crc shr 8 and 0xff
        return (i == toInt && i1 ==toInt1 )
    }

    companion object {
        private const val Head: Byte = 0xA5.toByte()
        private const val Head2: Byte = 0x5A
    }
}
